3d8d03
@@ -111,26 +111,20 @@
public void addBatch(String sql) throws SQLException {
 
   @Override
   public void cancel() throws SQLException {
-    if (isClosed) {
-      throw new SQLException("Can't cancel after statement has been closed");
-    }
-
-    if (stmtHandle == null) {
-      return;
-    }
+    checkConnection("cancel");
 
-    TCancelOperationReq cancelReq = new TCancelOperationReq();
-    cancelReq.setOperationHandle(stmtHandle);
+    transportLock.lock();
     try {
-      transportLock.lock();
-      TCancelOperationResp cancelResp = client.CancelOperation(cancelReq);
-      Utils.verifySuccessWithInfo(cancelResp.getStatus());
+      if (stmtHandle != null) {
+        TCancelOperationReq cancelReq = new TCancelOperationReq(stmtHandle);
+        TCancelOperationResp cancelResp = client.CancelOperation(cancelReq);
+        Utils.verifySuccessWithInfo(cancelResp.getStatus());
+      }
     } catch (SQLException e) {
       throw e;
     } catch (Exception e) {
       throw new SQLException(e.toString(), "08S01", e);
-    }
-    finally {
+    } finally {
       transportLock.unlock();
     }
   }
@@ -158,11 +152,10 @@
public void clearWarnings() throws SQLException {
   }
 
   void closeClientOperation() throws SQLException {
+    transportLock.lock();
     try {
       if (stmtHandle != null) {
-        TCloseOperationReq closeReq = new TCloseOperationReq();
-        closeReq.setOperationHandle(stmtHandle);
-        transportLock.lock();
+        TCloseOperationReq closeReq = new TCloseOperationReq(stmtHandle);
         TCloseOperationResp closeResp = client.CloseOperation(closeReq);
         Utils.verifySuccessWithInfo(closeResp.getStatus());
       }
@@ -170,8 +163,7 @@
void closeClientOperation() throws SQLException {
       throw e;
     } catch (Exception e) {
       throw new SQLException(e.toString(), "08S01", e);
-    }
-    finally {
+    } finally {
       transportLock.unlock();
     }
     stmtHandle = null;
@@ -187,16 +179,14 @@
public void close() throws SQLException {
     if (isClosed) {
       return;
     }
-    if (stmtHandle != null) {
-      closeClientOperation();
-    }
+    closeClientOperation();
     client = null;
     resultSet = null;
     isClosed = true;
   }
 
+  // JDK 1.7
   public void closeOnCompletion() throws SQLException {
-    // JDK 1.7
     throw new SQLException("Method not supported");
   }
 
@@ -208,25 +198,22 @@
public void closeOnCompletion() throws SQLException {
 
   @Override
   public boolean execute(String sql) throws SQLException {
-    if (isClosed) {
-      throw new SQLException("Can't execute after statement has been closed");
-    }
+    checkConnection("execute");
 
-    try {
-      if (stmtHandle != null) {
-        closeClientOperation();
-      }
+    closeClientOperation();
 
-      TExecuteStatementReq execReq = new TExecuteStatementReq(sessHandle, sql);
-      /**
-       * Run asynchronously whenever possible
-       * Currently only a SQLOperation can be run asynchronously,
-       * in a background operation thread
-       * Compilation is synchronous and execution is asynchronous
-       */
-      execReq.setRunAsync(true);
-      execReq.setConfOverlay(sessConf);
-      transportLock.lock();
+    TExecuteStatementReq execReq = new TExecuteStatementReq(sessHandle, sql);
+    /**
+     * Run asynchronously whenever possible
+     * Currently only a SQLOperation can be run asynchronously,
+     * in a background operation thread
+     * Compilation is synchronous and execution is asynchronous
+     */
+    execReq.setRunAsync(true);
+    execReq.setConfOverlay(sessConf);
+
+    transportLock.lock();
+    try {
       TExecuteStatementResp execResp = client.ExecuteStatement(execReq);
       Utils.verifySuccessWithInfo(execResp.getStatus());
       stmtHandle = execResp.getOperationHandle();
@@ -234,8 +221,7 @@
public boolean execute(String sql) throws SQLException {
       throw eS;
     } catch (Exception ex) {
       throw new SQLException(ex.toString(), "08S01", ex);
-    }
-    finally {
+    } finally {
       transportLock.unlock();
     }
 
@@ -253,11 +239,7 @@
public boolean execute(String sql) throws SQLException {
         transportLock.lock();
         try {
           statusResp = client.GetOperationStatus(statusReq);
-        }
-        catch (Exception e) {
-          throw e;
-        }
-        finally {
+        } finally {
           transportLock.unlock();
         }
         Utils.verifySuccessWithInfo(statusResp.getStatus());
@@ -300,6 +282,12 @@
public boolean execute(String sql) throws SQLException {
     return true;
   }
 
+  private void checkConnection(String action) throws SQLException {
+    if (isClosed) {
+      throw new SQLException("Can't " + action + " after statement has been closed");
+    }
+  }
+
   /*
    * (non-Javadoc)
    *
@@ -411,6 +399,7 @@
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
 
   @Override
   public Connection getConnection() throws SQLException {
+    checkConnection("getConnection");
     return this.connection;
   }
 
@@ -422,7 +411,8 @@
public Connection getConnection() throws SQLException {
 
   @Override
   public int getFetchDirection() throws SQLException {
-    throw new SQLException("Method not supported");
+    checkConnection("getFetchDirection");
+    return ResultSet.FETCH_FORWARD;
   }
 
   /*
@@ -433,6 +423,7 @@
public int getFetchDirection() throws SQLException {
 
   @Override
   public int getFetchSize() throws SQLException {
+    checkConnection("getFetchSize");
     return fetchSize;
   }
 
@@ -466,6 +457,7 @@
public int getMaxFieldSize() throws SQLException {
 
   @Override
   public int getMaxRows() throws SQLException {
+    checkConnection("getMaxRows");
     return maxRows;
   }
 
@@ -499,7 +491,8 @@
public boolean getMoreResults(int current) throws SQLException {
 
   @Override
   public int getQueryTimeout() throws SQLException {
-    throw new SQLException("Method not supported");
+    checkConnection("getQueryTimeout");
+    return 0;
   }
 
   /*
@@ -510,6 +503,7 @@
public int getQueryTimeout() throws SQLException {
 
   @Override
   public ResultSet getResultSet() throws SQLException {
+    checkConnection("getResultSet");
     return resultSet;
   }
 
@@ -543,7 +537,8 @@
public int getResultSetHoldability() throws SQLException {
 
   @Override
   public int getResultSetType() throws SQLException {
-    throw new SQLException("Method not supported");
+    checkConnection("getResultSetType");
+    return ResultSet.TYPE_FORWARD_ONLY;
   }
 
   /*
@@ -554,6 +549,7 @@
public int getResultSetType() throws SQLException {
 
   @Override
   public int getUpdateCount() throws SQLException {
+    checkConnection("getUpdateCount");
     return 0;
   }
 
@@ -565,6 +561,7 @@
public int getUpdateCount() throws SQLException {
 
   @Override
   public SQLWarning getWarnings() throws SQLException {
+    checkConnection("getWarnings");
     return warningChain;
   }
 
@@ -579,9 +576,9 @@
public boolean isClosed() throws SQLException {
     return isClosed;
   }
 
+  // JDK 1.7
   public boolean isCloseOnCompletion() throws SQLException {
-    // JDK 1.7
-    throw new SQLException("Method not supported");
+    return false;
   }
 
   /*
@@ -592,7 +589,7 @@
public boolean isCloseOnCompletion() throws SQLException {
 
   @Override
   public boolean isPoolable() throws SQLException {
-    throw new SQLException("Method not supported");
+    return false;
   }
 
   /*
@@ -625,7 +622,10 @@
public void setEscapeProcessing(boolean enable) throws SQLException {
 
   @Override
   public void setFetchDirection(int direction) throws SQLException {
-    throw new SQLException("Method not supported");
+    checkConnection("setFetchDirection");
+    if (direction != ResultSet.FETCH_FORWARD) {
+      throw new SQLException("Not supported direction " + direction);
+    }
   }
 
   /*
@@ -636,6 +636,7 @@
public void setFetchDirection(int direction) throws SQLException {
 
   @Override
   public void setFetchSize(int rows) throws SQLException {
+    checkConnection("setFetchSize");
     fetchSize = rows;
   }
 
@@ -658,6 +659,7 @@
public void setMaxFieldSize(int max) throws SQLException {
 
   @Override
   public void setMaxRows(int max) throws SQLException {
+    checkConnection("setMaxRows");
     if (max < 0) {
       throw new SQLException("max must be >= 0");
     }
@@ -694,7 +696,7 @@
public void setQueryTimeout(int seconds) throws SQLException {
 
   @Override
   public boolean isWrapperFor(Class<?> iface) throws SQLException {
-    throw new SQLException("Method not supported");
+    return false;
   }
 
   /*
@@ -705,7 +707,7 @@
public boolean isWrapperFor(Class<?> iface) throws SQLException {
 
   @Override
   public <T> T unwrap(Class<T> iface) throws SQLException {
-    throw new SQLException("Method not supported");
+    throw new SQLException("Cannot unwrap to " + iface);
   }
 
 }
